package com.linux.huhx.thread;

import java.util.concurrent.TimeUnit;

/**
 * Created by huhx on 2017-05-24.
 */
public class ThreadYieldTest {
    public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    if (i % 2 == 0) {
                        Thread.yield();
                    }
                    System.out.println("thread 1 " + i);
                }
            }
        }.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    if (i % 2 == 1) {
                        Thread.yield();
                    }
                    System.out.println("thread 2 " + i);
                }
            }
        }).start();
        for (int i = 0; i < 5; i++) {
            System.out.println("main " + i);
//            try {
//                TimeUnit.MILLISECONDS.sleep(1000);
//                System.out.println("main " + i);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }
}

